package com.kitty.game.party.service;

import com.kitty.game.party.model.Party;
import com.kitty.game.role.model.Role;
import com.kitty.game.config.Server;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.model.product.ActivityType;
import com.kitty.game.chat.message.RespChat;
import com.kitty.game.i18n.I18nId;
import com.kitty.game.i18n.I18nIdDataPool;
import com.kitty.game.party.message.RespPartyRecvRedBag;
import com.kitty.game.party.message.RespPartyRedBagList;
import com.kitty.game.party.message.RespPartyRedBagRecord;
import com.kitty.game.party.message.RespRedBagSendInfo;
import com.kitty.game.party.message.vo.RecvRedBagInfo;
import com.kitty.game.party.message.vo.RedBagInfo;
import com.kitty.game.party.model.FetchRedBagRecord;
import com.kitty.game.party.model.PartyMemberObject;
import com.kitty.game.party.model.PartyRedBag;
import com.kitty.game.party.model.RedBagRecord;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.*;
import com.kitty.common.model.Reason;
import com.kitty.mina.message.MessagePusher;
import com.kitty.game.ServerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
//后加
import com.kitty.game.utils.FieldUtils;
import com.kitty.game.utils.SenderUtils;

@Component
@Slf4j
public class PartyRedBagHandler {

    @Autowired
    private PartyService partyService;

    /**帮派红包配置*/
    public void sendRedBagSetting(Role role) {
        RespRedBagSendInfo respRedBagSendInfo = getRespRedBagSendInfo(role);
        MessagePusher.pushMessage(role, respRedBagSendInfo);
    }

    private RespRedBagSendInfo getRespRedBagSendInfo(Role role) {
        RespRedBagSendInfo respRedBagSendInfo = new RespRedBagSendInfo();
        int remainCount = SpringUtils.getActivityService().getRemainCount(role, ActivityType.PARTY_SEND_BAG);
        respRedBagSendInfo.setLeftTimes((short) remainCount);
        Party party = partyService.getParty(role);
        short memberCount = partyService.getMemberCount(party);
        respRedBagSendInfo.setMemberCount(memberCount);
        respRedBagSendInfo.setActiviesInfoList(new ArrayList<>());
        return respRedBagSendInfo;
    }

    /**发帮派红包*/
    public void sendRedBag(Role role, short gold, short num, String msg, String format) {
        Party party = partyService.getParty(role);
        if (party == null) {
            MessagePusher.notify2Player(role, I18nId.PMT_319);
            return ;
        }

        synchronized (party) {
            /**检测*/
            String check = checkSendRedBag(role, gold, num);
            if (StringUtils.isNotEmpty(check)) {
                MessagePusher.pushMessage(role, new RespMsg(check));
                return ;
            }

            /**实际发红包*/
            /**扣除金元宝*/
            SpringUtils.getRoleService().subtractGold(role, gold, Reason.PARTY_SEND_RED_BAG);

            /**添加红包*/
            PartyRedBag partyRedBag = addPartyRedBag(role, party, gold, num, msg);
            if (partyRedBag == null) {return ;}

            /**添加次数*/
            SpringUtils.getActivityService().addFinishCount(role, ActivityType.PARTY_SEND_BAG, 1);

            /**记录发红包数据*/
            addMemberSendRecord(role, party, partyRedBag);

            /**通知在线帮派成员抢红包*/

            /**帮派聊天发红包信息*/
            String formatMsg = String.format(format, partyRedBag.getId(), 1, party.getId(), msg);
            sendPartyChat(role, formatMsg, true);

            /**返回红包列表*/
            RespPartyRedBagList respPartyRedBagList = getRespPartyRedBagList(role);
            MessagePusher.pushMessage(role, respPartyRedBagList);

            /**提示成功*/
            MessagePusher.notify2Player(role, I18nId.PMT_390);
        }
    }



    /**发帮派红包前检测*/
    private String checkSendRedBag(Role role, short gold, short num) {
        /**检测金元宝是否在范围之内(1000~30000)*/
        if (gold < Const.RED_BAG_MIN_COUNT) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_383, Const.RED_BAG_MIN_COUNT);
        }
        if (gold > Const.RED_BAG_MAX_COUNT) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_384, Const.RED_BAG_MAX_COUNT);
        }
        /**检测红包数量是否在范围之内*/
        int minNum = getMinNum(gold);
        int maxNum = getMaxNum(gold);
        if (num < minNum) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_385, minNum);
        }
        if (num > maxNum) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_386, maxNum);
        }

        /**检测金元宝数量是否足够*/
        if (role.getGold() < gold) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_403);
        }

        /**检测人物等级是否在50级以上*/
        if (role.getLevel() < Const.RED_BAG_MIN_LEVEL) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_387, Const.RED_BAG_MIN_LEVEL);
        }
        Party party = partyService.getParty(role);
        /**检测是否还有发红包剩余次数（每日8次）*/
        int remainCount = SpringUtils.getActivityService().getRemainCount(role, ActivityType.PARTY_SEND_BAG);
        if (remainCount <= 0) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_389);
        }

        /**检测是否帮派成员数量足够(20人以上)*/
        short memberCount = partyService.getMemberCount(party);
        if (memberCount < Const.RED_BAG_MIN_MEMBER_COUNT) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_388, Const.RED_BAG_MIN_MEMBER_COUNT);
        }

        return null;
    }

    /**获得对应元宝的最大红包个数*/
    private int getMaxNum(int gold) {
        return (int) Math.min(100, Math.floor(gold  * 3 / 300));
    }

    /**获得对应元宝的最小红包个数*/
    private int getMinNum(int gold) {
        return (int) Math.max(Math.floor(gold /600), 10);
    }

    /**添加帮派红包*/
    private PartyRedBag addPartyRedBag(Role role, Party party, short gold, short num, String msg) {
        if (role == null || party == null) {return null;}

        /**生成帮派红包数据*/
        PartyRedBag partyRedBag = new PartyRedBag(role.getUid(), role.getName(), gold, num, msg);
        if (party.getRedBagList() == null) {
            party.setRedBagList(new ArrayList<>());
        }
        party.getRedBagList().add(0, partyRedBag);
        /**超过最大数量时，删除超过数量的红包数据*/
        int size = party.getRedBagList().size();
        if (size > Const.RED_BAG_MAX_SEND_COUNT) {
            for (int i=0, loop=size-Const.RED_BAG_MAX_SEND_COUNT; i<loop; i++) {
                party.getRedBagList().remove(party.getRedBagList().size()-1);
            }
        }
        party.save();

        return partyRedBag;
    }

    /**添加成员个人发红包记录*/
    private void addMemberSendRecord(Role role, Party party, PartyRedBag partyRedBag) {
        PartyMemberObject partyMember = partyService.getPartyMember(party, role);
        if (partyMember.getSendRedBagRecords() == null) {
            partyMember.setSendRedBagRecords(new ArrayList<>());
        }

        int gold = partyRedBag.getSiver();
        RedBagRecord redBagRecord = new RedBagRecord(partyRedBag.getId(), partyRedBag.getSenderName(), gold);
        partyMember.getSendRedBagRecords().add(redBagRecord);
        party.save();
    }

    /**获得红包列表*/
    private RespPartyRedBagList getRespPartyRedBagList(Role role) {
        Party party = partyService.getParty(role);
        RespPartyRedBagList respPartyRedBagList = new RespPartyRedBagList();
        List<RedBagInfo> redBagInfoList = new ArrayList<>();
        respPartyRedBagList.setRedBagInfoList(redBagInfoList);

        List<PartyRedBag> redBagList = party.getRedBagList();
        if (redBagList != null) {
            RedBagInfo redBagInfo = null;
            for (PartyRedBag partyRedBag : redBagList) {
                PartyMemberObject partyMember = party.getMembers().get(partyRedBag.getSenderUid());
                if (partyMember == null) {continue;}

                redBagInfo = new RedBagInfo();
                redBagInfo.setRedBgGid(String.valueOf(partyRedBag.getId()));
                redBagInfo.setSenderName(partyMember.getRoleName());
                redBagInfo.setMsg(partyRedBag.getMsg());
                redBagInfo.setTime(partyRedBag.getTime());
                redBagInfo.setCount(partyRedBag.getCount());
                byte state = (byte) (isRedBagNotExpire(partyRedBag) ? 1 : 0);
                redBagInfo.setState(state);
                byte isSender = (byte) (isRedBagSender(role, partyRedBag) ? 1 : 0);
                redBagInfo.setIsSender(isSender);
                byte isRecv = (byte) (isRedBagFetched(role, partyRedBag) ? 1 : 0);
                redBagInfo.setIsRecv(isRecv);

                redBagInfoList.add(redBagInfo);

                /**只显示一定数量*/
                if (redBagInfoList.size() >= Const.RED_BAG_SHOW_COUNT) {
                    break;
                }
            }
        }
        return respPartyRedBagList;
    }

    /**帮派红包是否已超时*/
    private boolean isRedBaExpire(PartyRedBag partyRedBag) {
        return (System.currentTimeMillis() - partyRedBag.getTime()* TimeUtil.ONE_SECOND) >= Const.RED_BAG_EXPIRE_TIME;
    }

    /**帮派红包是否未超时*/
    private boolean isRedBagNotExpire(PartyRedBag partyRedBag) {
        return !isRedBaExpire(partyRedBag);
    }

    /**帮派红包是否由该玩家发送*/
    private boolean isRedBagSender(Role role, PartyRedBag partyRedBag) {
        if (role == null || partyRedBag == null) {return false;}

        return partyRedBag.getSenderUid() == role.getUid();
    }

    /**是否已经领取红包*/
    private boolean isRedBagFetched(Role role, PartyRedBag partyRedBag) {
        if (role == null || partyRedBag == null) {
            return false;
        }
        return partyRedBag.getFetchRedBagRecordMap() != null && partyRedBag.getFetchRedBagRecordMap().containsKey(role.getUid());
    }

    /**发帮派聊天*/
    public void sendPartyChat(Role role, String msg, boolean isRoleSend) {
        RespChat respChat = new RespChat();
        respChat.setMsg(msg);
        respChat.setList(isRoleSend ? FieldUtils.getChatList(role) : new ArrayList<>());
        respChat.setRoleName(isRoleSend ? role.getName() : null);
        Server server = SpringUtils.getBean(ServerService.class).getServer();
        respChat.setLineName(server.getSonName());
        respChat.setRoleId(isRoleSend ? role.getRoleId() : 0);
        respChat.setTime((int) (System.currentTimeMillis() / TimeUtil.ONE_SECOND));
        respChat.setType(ChatConst.PARTY);
        respChat.setPartyName(role.getPartyName());

        SenderUtils.pushPartyChatMessage(role.getPartyId(), respChat);
    }

    /**显示红包列表*/
    public void showRedBagList(Role role) {
        RespPartyRedBagList respPartyRedBagList = getRespPartyRedBagList(role);
        MessagePusher.pushMessage(role, respPartyRedBagList);
    }

    /**显示对应红包id的红包信息*/
    public void showRedBag(Role role, long redBagGid) {
        RespPartyRecvRedBag respPartyRecvRegBag = getRespPartyRecvRedBag(role, redBagGid, false);
        MessagePusher.pushMessage(role, respPartyRecvRegBag);
    }

    /**获得对应红包id的红包信息*/
    private RespPartyRecvRedBag getRespPartyRecvRedBag(Role role, long redBagGid, boolean isFetchOper) {
        Party party = partyService.getParty(role);
        PartyRedBag partyRedBag = getPartyRedBag(party, redBagGid);
        if (partyRedBag == null) {
            MessagePusher.notify2Player(role, I18nId.PMT_395, Const.RED_BAG_KEEP_DAY);
            return null;
        }


        RespPartyRecvRedBag respPartyRecvRegBag = new RespPartyRecvRedBag();
        FetchRedBagRecord roleFetchRedBagRecord = null;
        if (partyRedBag.getFetchRedBagRecordMap() != null) {
            roleFetchRedBagRecord = partyRedBag.getFetchRedBagRecordMap().get(role.getUid());
        }
        byte type = 0;
        if (isFetchOper) {
            type = (byte) (roleFetchRedBagRecord != null ? 3 : 2);
        } else {
            type = (byte) 1;
        }
        respPartyRecvRegBag.setType(type);
        respPartyRecvRegBag.setTotalCoin(partyRedBag.getSiver());
        respPartyRecvRegBag.setCoin(roleFetchRedBagRecord != null ? roleFetchRedBagRecord.getCoin() : 0);
        respPartyRecvRegBag.setRedbagGid(String.valueOf(redBagGid));
        PartyMemberObject partyMember = party.getMembers().get(partyRedBag.getSenderUid());
        respPartyRecvRegBag.setSenderName(partyMember != null ? partyMember.getRoleName() : "");
        respPartyRecvRegBag.setMsg(partyRedBag.getMsg());
        respPartyRecvRegBag.setSendTime(partyRedBag.getTime());
        respPartyRecvRegBag.setCount(partyRedBag.getCount());
        byte state = (byte) (isRedBagNotExpire(partyRedBag) ? 1 : 0);
        respPartyRecvRegBag.setState(state);
        byte isSender = (byte) (isRedBagSender(role, partyRedBag) ? 1 : 0);
        respPartyRecvRegBag.setIsSender(isSender);
        byte isRecv = (byte) (isRedBagFetched(role, partyRedBag) ? 1 : 0);
        respPartyRecvRegBag.setIsRecv(isRecv);
        List<RecvRedBagInfo> recvRedBagInfoList = new ArrayList<>();
        respPartyRecvRegBag.setRecvRedBagInfoList(recvRedBagInfoList);
        if (partyRedBag.getFetchRedBagRecordMap() != null) {
            for (FetchRedBagRecord fetchRedBagRecord : partyRedBag.getFetchRedBagRecordMap().values()) {
                PartyMemberObject partyMemberObject = party.getMembers().get(fetchRedBagRecord.getRoleUid());
                if (partyMemberObject == null) {continue;}

                RecvRedBagInfo recvRedBagInfo = new RecvRedBagInfo();
                recvRedBagInfo.setName(partyMemberObject.getRoleName());
                recvRedBagInfo.setCoin(fetchRedBagRecord.getCoin());
                recvRedBagInfo.setTime(fetchRedBagRecord.getTime());
                recvRedBagInfoList.add(recvRedBagInfo);
            }
        }
        return respPartyRecvRegBag;
    }

    /**获得对应红包id的红包对象*/
    private PartyRedBag getPartyRedBag(Party party, long redBagGid) {
        if (party == null || party.getRedBagList() == null) {return null;}

        for (PartyRedBag partyRedBag : party.getRedBagList()) {
            if (partyRedBag.getId() == redBagGid) {
                return partyRedBag;
            }
        }
        return null;
    }

    /**领取红包*/
    public void fetchRedBag(Role role, long redBagGid) {
        Party party = partyService.getParty(role);
        if (party == null) {
            MessagePusher.notify2Player(role, I18nId.PMT_393);
            return ;
        }

        synchronized (party) {
            PartyRedBag partyRedBag = getPartyRedBag(party, redBagGid);
            /**假设不能找到都是已经过期了（不改包的数据正常是这样的）*/
            if (partyRedBag == null) {
                MessagePusher.notify2Player(role, I18nId.PMT_395, Const.RED_BAG_KEEP_DAY);
                return ;
            }

            if (isRedBagFetched(role, partyRedBag)) {
                RespPartyRecvRedBag respPartyRecvRegBag = getRespPartyRecvRedBag(role, redBagGid, false);
                MessagePusher.pushMessage(role, respPartyRecvRegBag);
                return ;
            }

            /**检测*/
            String check = checkFetchRedBag(role, partyRedBag);
            if (StringUtils.isNotEmpty(check)) {
                MessagePusher.pushMessage(role, new RespMsg(check));
                return ;
            }

            doFetchRedBag(role, partyRedBag);

            RespPartyRecvRedBag respPartyRecvRegBag = getRespPartyRecvRedBag(role, redBagGid, true);
            MessagePusher.pushMessage(role, respPartyRecvRegBag);
        }
    }

    private String checkFetchRedBag(Role role, PartyRedBag partyRedBag) {
        /**检测*/
        /**检测人物等级是否在50级以上*/
        if (role.getLevel() < Const.RED_BAG_MIN_LEVEL) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_392, Const.RED_BAG_MIN_LEVEL);
        }
        /**检测是否已经领取*/
        /**检测是否已经领取完*/
        /**检测是否在领取时间范围内(超过6小时不能再领取)*/
        /**红包已超时*/
        if (isRedBaExpire(partyRedBag)) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_394);
        }

        /**检测是否在帮派中*/
        Party party = partyService.getParty(role);
        if (party == null) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_393);
        }

        return null;
    }



    /**领取帮派红包*/
    private void doFetchRedBag(Role role, PartyRedBag partyRedBag) {
        short remainCount = partyRedBag.getRemainCount();
        if (remainCount <= 0 || partyRedBag.getRemainSiver() <= 0) {
            return ;
        }
        if (isRedBagFetched(role, partyRedBag)) {
            return ;
        }
        /**红包已超时*/
        if (isRedBaExpire(partyRedBag)) {
            return ;
        }

        /**抢红包*/
        short randomSiver = getRandomSiver(partyRedBag);
        SpringUtils.getRoleService().addSiver(role, randomSiver, Reason.PARTY_RECV_RED_BAG);
        log.warn("addSiver uid:{},siver:{},remainCount:{}，remainSiver:{}", role.getUid(), randomSiver, partyRedBag.getRemainCount(), partyRedBag.getRemainSiver());

        addFetchRedBagRecord(role, partyRedBag, randomSiver);

        Party party = partyService.getParty(role);
        /**记录抢红包数据*/
        addMemberRecvRecord(role, partyRedBag, randomSiver, party);

        /**帮派聊天发领取信息*/
        PartyMemberObject sendPartyMember = party.getMembers().get(partyRedBag.getSenderUid());
        String content = I18nIdDataPool.getI18nContent(I18nId.PMT_391, role.getName(), sendPartyMember.getRoleName());
        sendPartyChat(role, content, false);

        party.save();
    }

    private short getRandomSiver(PartyRedBag partyRedBag) {
        /**随机额度*/
        /**根据剩余额度、剩余红包数获得，只剩1个时则直接返回剩余额度，如果大于1个，则在剩余额度中随机*/
        /**更新剩余额度和剩余红包数*/
        short siver = 0;
        if (partyRedBag.getRemainCount() == 1) {
            siver = partyRedBag.getRemainSiver();
        } else {
            int min = 10;
            int max = (partyRedBag.getRemainSiver() - partyRedBag.getRemainCount() * min) / partyRedBag.getRemainCount();
            siver = (short) ThreadLocalRandom.current().nextInt(min, max);
        }
        partyRedBag.setRemainCount((short) (partyRedBag.getRemainCount()-1));
        partyRedBag.setRemainSiver((short) (partyRedBag.getRemainSiver() - siver));

        return siver;
    }

    /**红包中添加领取红包记录*/
    private void addFetchRedBagRecord(Role role, PartyRedBag partyRedBag, short randomSiver) {
        FetchRedBagRecord fetchRedBagRecord = new FetchRedBagRecord(role.getUid(), randomSiver);
        if (partyRedBag.getFetchRedBagRecordMap() == null) {
            partyRedBag.setFetchRedBagRecordMap(new HashMap<>());
        }
        partyRedBag.getFetchRedBagRecordMap().put(fetchRedBagRecord.getRoleUid(), fetchRedBagRecord);
    }

    /**添加成员个人领取红包记录*/
    private void addMemberRecvRecord(Role role, PartyRedBag partyRedBag, short siver, Party party) {
        PartyMemberObject partyMember = partyService.getPartyMember(party, role);
        if (partyMember.getRecvRedBagRecords() == null) {
            partyMember.setRecvRedBagRecords(new ArrayList<>());
        }
        RedBagRecord recvRedBagRecord = new RedBagRecord(partyRedBag.getId(), partyRedBag.getSenderName(), siver);
        partyMember.getRecvRedBagRecords().add(recvRedBagRecord);
    }

    public void showRedBagRecord(Role role, byte type) {
        Party party = partyService.getParty(role);
        PartyMemberObject partyMember = partyService.getPartyMember(party, role);

        if (type == 1) {
            /**发红包记录*/
            RespPartyRedBagRecord respPartyRedBagRecord = getRespSendRedBagRecord(type, partyMember);
            MessagePusher.pushMessage(role, respPartyRedBagRecord);
        } else if (type == 0) {
            /**抢红包记录*/
            RespPartyRedBagRecord respPartyRedBagRecord = getRespRecvRedBagRecord(type, partyMember);
            MessagePusher.pushMessage(role, respPartyRedBagRecord);
        }
    }

    /**获得发红包记录返回数据*/
    private RespPartyRedBagRecord getRespSendRedBagRecord(byte type, PartyMemberObject partyMember) {
        RespPartyRedBagRecord respPartyRedBagRecord = new RespPartyRedBagRecord();
        respPartyRedBagRecord.setType(type);
        int total = 0;
        int count = 0;
        List<RecvRedBagInfo> redBagInfoList = new ArrayList<>();
        if (partyMember.getSendRedBagRecords() != null) {
            for (int i=partyMember.getSendRedBagRecords().size()-1; i>=0; i--) {
                RedBagRecord redBagRecord = partyMember.getSendRedBagRecords().get(i);
                RecvRedBagInfo recvRedBagInfo = new RecvRedBagInfo();
                /**发红包的额度*/
                recvRedBagInfo.setCoin(redBagRecord.getCoin());
                /**发红包的时间*/
                recvRedBagInfo.setTime(redBagRecord.getTime());
                recvRedBagInfo.setName(redBagRecord.getSenderName());
                redBagInfoList.add(recvRedBagInfo);

                total += redBagRecord.getCoin();
                count += 1;
            }
        }

        respPartyRedBagRecord.setTotal(total);
        respPartyRedBagRecord.setCount(count);
        respPartyRedBagRecord.setRecvRedBagInfoList(redBagInfoList);
        return respPartyRedBagRecord;
    }

    /**获得抢红包记录返回数据*/
    private RespPartyRedBagRecord getRespRecvRedBagRecord(byte type, PartyMemberObject partyMember) {
        RespPartyRedBagRecord respPartyRedBagRecord = new RespPartyRedBagRecord();
        respPartyRedBagRecord.setType(type);
        int total = 0;
        int count = 0;
        List<RecvRedBagInfo> redBagInfoList = new ArrayList<>();
        if (partyMember.getRecvRedBagRecords() != null) {
            for (int i=partyMember.getRecvRedBagRecords().size()-1; i>=0; i--) {
                RedBagRecord redBagRecord = partyMember.getRecvRedBagRecords().get(i);
                RecvRedBagInfo recvRedBagInfo = new RecvRedBagInfo();
                /**抢红包的额度*/
                recvRedBagInfo.setCoin(redBagRecord.getCoin());
                /**抢红包的时间*/
                recvRedBagInfo.setTime(redBagRecord.getTime());
                recvRedBagInfo.setName(redBagRecord.getSenderName());
                redBagInfoList.add(recvRedBagInfo);

                total += redBagRecord.getCoin();
                count += 1;
            }
        }
        respPartyRedBagRecord.setTotal(total);
        respPartyRedBagRecord.setCount(count);
        respPartyRedBagRecord.setRecvRedBagInfoList(redBagInfoList);
        return respPartyRedBagRecord;
    }

    /**检测帮派红包*/
    public void checkPartyRedBag(Party party) {
        if (party.getRedBagList() == null) {return ;}

        /**超过1个星期发的红包删除数据*/
        List<PartyRedBag> removeList = new ArrayList<>();
        for (PartyRedBag partyRedBag : party.getRedBagList()) {
            if ((System.currentTimeMillis() - partyRedBag.getTime() * TimeUtil.ONE_SECOND) > Const.RED_BAG_KEEP_DAY * TimeUtil.ONE_DAY) {
                removeList.add(partyRedBag);
            }
        }

        if (removeList.size() > 0) {
            synchronized (party) {
                for (PartyRedBag partyRedBag : removeList) {
                    party.getRedBagList().remove(partyRedBag);
                }
                party.save();
            }
        }
    }

    /**删除玩家对应发的帮派红包*/
    public void delPartyRedBag(Role role) {
        if (role == null) {return ;}

        Party party = partyService.getParty(role);
        if (party == null) {return ;}

        if (party.getRedBagList() == null) {return;}

        List<PartyRedBag> removeList = new ArrayList<>();
        for (PartyRedBag partyRedBag : party.getRedBagList()) {
            if (partyRedBag.getSenderUid() == role.getUid()) {
                removeList.add(partyRedBag);
            }
        }

        if (removeList.size() > 0) {
            synchronized (party) {
                for (PartyRedBag partyRedBag : removeList) {
                    party.getRedBagList().remove(partyRedBag);
                }
                party.save();
            }
        }
    }
}
